unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, OleCtrls, SHDocVw, Sockets, ComCtrls, Menus, StdCtrls,
  Buttons,inifiles,ActiveX;

type
  TMainForm = class(TForm)
    StatusBar1: TStatusBar;
    TcpServer1: TTcpServer;
    WebBrowser: TWebBrowser;
    Panel1: TPanel;
    Panel3: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Panel2: TPanel;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    New1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    SaveAs1: TMenuItem;
    N2: TMenuItem;
    Print1: TMenuItem;
    PrintSetup1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    Navigate1: TMenuItem;
    ShowIPAddress1: TMenuItem;
    Show1: TMenuItem;
    Hidden1: TMenuItem;
    View1: TMenuItem;
    Help1: TMenuItem;
    About1: TMenuItem;
    PrintPreview1: TMenuItem;
    Back1: TMenuItem;
    Forward1: TMenuItem;
    Stop1: TMenuItem;
    Refresh1: TMenuItem;
    Home1: TMenuItem;
    Search1: TMenuItem;
    N3: TMenuItem;
    SetHomePage1: TMenuItem;
    N4: TMenuItem;
    Zoomin1: TMenuItem;
    N501: TMenuItem;
    N1001: TMenuItem;
    N2001: TMenuItem;
    ZoomOut1: TMenuItem;
    N502: TMenuItem;
    N1002: TMenuItem;
    N2002: TMenuItem;
    RealSize1: TMenuItem;
    N5: TMenuItem;
    FindSomeWord1: TMenuItem;
    N6: TMenuItem;
    Scrolling1: TMenuItem;
    ScrollUp1: TMenuItem;
    ScrollDown1: TMenuItem;
    ScrollLeft1: TMenuItem;
    ScrollRight1: TMenuItem;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    CMBADDRESS: TComboBox;
    BitBtn3: TBitBtn;
    PageInformaiton1: TMenuItem;
    DisplayTitle1: TMenuItem;
    DocumentReferrer1: TMenuItem;
    ShowLinks1: TMenuItem;
    ListBox1: TListBox;
    rue1: TMenuItem;
    False1: TMenuItem;
    Caution1: TMenuItem;
    WorkOfline1: TMenuItem;
    N7: TMenuItem;
    Enadeled1: TMenuItem;
    Disabled1: TMenuItem;
    DeleteHistory1: TMenuItem;
    SaveDialog1: TSaveDialog;
    procedure FormResize(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Show1Click(Sender: TObject);
    procedure Hidden1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure New1Click(Sender: TObject);
    procedure Print1Click(Sender: TObject);
    procedure PrintSetup1Click(Sender: TObject);
    procedure PrintPreview1Click(Sender: TObject);
    procedure WebBrowserDocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure Search1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure Back1Click(Sender: TObject);
    procedure Forward1Click(Sender: TObject);
    procedure Stop1Click(Sender: TObject);
    procedure Refresh1Click(Sender: TObject);
    procedure Home1Click(Sender: TObject);
    procedure N501Click(Sender: TObject);
    procedure N1001Click(Sender: TObject);
    procedure RealSize1Click(Sender: TObject);
    procedure N2001Click(Sender: TObject);
    procedure N502Click(Sender: TObject);
    procedure N1002Click(Sender: TObject);
    procedure N2002Click(Sender: TObject);
    procedure ScrollUp1Click(Sender: TObject);
    procedure ScrollDown1Click(Sender: TObject);
    procedure ScrollLeft1Click(Sender: TObject);
    procedure ScrollRight1Click(Sender: TObject);
    procedure CMBADDRESSKeyPress(Sender: TObject; var Key: Char);
    procedure CMBADDRESSSelect(Sender: TObject);
    procedure WebBrowserDownloadComplete(Sender: TObject);
    procedure WebBrowserDownloadBegin(Sender: TObject);
    procedure WebBrowserProgressChange(Sender: TObject; Progress,
      ProgressMax: Integer);
    procedure FindSomeWord1Click(Sender: TObject);
    procedure About1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure DisplayTitle1Click(Sender: TObject);
    procedure DocumentReferrer1Click(Sender: TObject);
    procedure rue1Click(Sender: TObject);
    procedure False1Click(Sender: TObject);
    procedure SetHomePage1Click(Sender: TObject);
    procedure Caution1Click(Sender: TObject);
    procedure Enadeled1Click(Sender: TObject);
    procedure Disabled1Click(Sender: TObject);
    procedure DeleteHistory1Click(Sender: TObject);
    procedure SaveAs1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);

 private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  i:integer;
  ini:tinifile;
implementation

uses Unit2, Unit3, Unit4, Unit5;

{$R *.dfm}

/////////////////////////////////////////////////////
(*Save Page*)
procedure WB_SaveAs_HTML(WB:TWebBrowser; const FileName : string);
var
PersistStream: IPersistStreamInit;
Stream: IStream;
FileStream: TFileStream;
begin
if not Assigned(WB.Document) then
begin
ShowMessage('Document not loaded!');
Exit;
end;
PersistStream := WB.Document as IPersistStreamInit;
FileStream := TFileStream.Create(FileName, fmCreate);
try
Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
if Failed(PersistStream.Save(Stream, True)) then
ShowMessage('SaveAs HTML fail!');
finally
FileStream.Free;
end;
end;
(*save Page*)
///////////////////////////////////////////////////////////
procedure TMainForm.FormResize(Sender: TObject);
begin
  cmbaddress.Width:=mainform.Width-88
end;

procedure TMainForm.BitBtn1Click(Sender: TObject);
begin
  edit2.Text := tcpserver1.LookupHostName(edit1.text);
end;

procedure TMainForm.BitBtn2Click(Sender: TObject);
begin
  edit1.Text := tcpserver1.LookupHostAddr(edit2.text);
end;

procedure TMainForm.Show1Click(Sender: TObject);
begin
  Panel3.Visible:=true;
  hidden1.Enabled:=true;
  show1.Enabled:=false;
end;

procedure TMainForm.Hidden1Click(Sender: TObject);
begin
  Panel3.Visible:=False;
  hidden1.Enabled:=False;
  show1.Enabled:=true;
end;

procedure TMainForm.Exit1Click(Sender: TObject);
begin
  application.Terminate;
end;

procedure TMainForm.New1Click(Sender: TObject);
  //Creat a new form
  var
  ms:tmemorystream;
  newform:TMainForm;
  begin
  ms:= tmemorystream.Create;
  try
  ms.WriteComponent(mainform);
  newform:=TMainForm.CreateNew(application);
  ms.Position:=0;
  ms.ReadComponent(newform);
  newform.Show;
  finally
  ms.Free
  end;
  //creat a new form
  newform.Top:=newform.Top+50;
  newform.Left:=newform.Left+50;
  end;

procedure TMainForm.Print1Click(Sender: TObject);
  var
  vain,vaout:olevariant;
  begin
  WEBBROWSER.ControlInterface.ExecWB(olecmdid_print,olecmdexecopt_promptuser,vain,vaout);
end;

procedure TMainForm.PrintSetup1Click(Sender: TObject);
  var
  vain,vaout:olevariant;
  begin
  webbrowser.ControlInterface.ExecWB(olecmdid_pagesetup,olecmdexecopt_promptuser,vain,vaout);
end;

procedure TMainForm.PrintPreview1Click(Sender: TObject);
  var
  vain,vaout:olevariant;
  begin
  webbrowser.ControlInterface.ExecWB(olecmdid_printpreview,olecmdexecopt_dontpromptuser,vain,vaout);
  end;

procedure TMainForm.WebBrowserDocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  print1.Enabled:=true;
  displaytitle1.Enabled:=true;
  DocumentReferrer1.Enabled:=true;
  printsetup1.Enabled:=true;
  printpreview1.Enabled:=true;
  ShowLinks1.Enabled:=true;
  n501.Enabled:=true;
  n502.Enabled:=true;
  n1001.Enabled:=true;
  n1002.Enabled:=true;
  n2001.Enabled:=true;
  n2002.Enabled:=true;
  scrollup1.Enabled:=true;
  scrolldown1.Enabled:=true;
  scrollleft1.Enabled:=true;
  scrollright1.Enabled:=true;
  findsomeword1.Enabled:=true;
      statusbar1.Panels[0].Text:='Page Loading Complete.';
end;

procedure TMainForm.Search1Click(Sender: TObject);
begin
  webbrowser.Navigate('Http://www.Google.com');
  cmbaddress.Text:='http://www.google.com'
end;

procedure TMainForm.SpeedButton1Click(Sender: TObject);
begin
  TRY
  webbrowser.GoBack;
  except
end;
end;

procedure TMainForm.SpeedButton2Click(Sender: TObject);
begin
  try
  webbrowser.GoForward;
  except
  end;
end;

procedure TMainForm.SpeedButton3Click(Sender: TObject);
begin
  try
  webbrowser.Refresh;
  except
  end;
end;

procedure TMainForm.SpeedButton4Click(Sender: TObject);
begin
  try
  webbrowser.Stop;
  except
  end;
end;

procedure TMainForm.SpeedButton5Click(Sender: TObject);
begin
cmbaddress.Text:= (sethome.Edit1.Text);
try
webbrowser.Navigate(sethome.Edit1.Text);
except
end;
end;

procedure TMainForm.SpeedButton6Click(Sender: TObject);
begin
  webbrowser.Navigate('Http://www.Google.com');
  cmbaddress.Text:='http://www.google.com'
end;

procedure TMainForm.Back1Click(Sender: TObject);
begin
  TRY
  webbrowser.GoBack;
  except
end;
end;

procedure TMainForm.Forward1Click(Sender: TObject);
begin
  TRY
  webbrowser.GoForward;
  except
end;
end;

procedure TMainForm.Stop1Click(Sender: TObject);
begin
  TRY
  webbrowser.Stop;
  except
end;
end;

procedure TMainForm.Refresh1Click(Sender: TObject);
begin
  TRY
  webbrowser.Refresh;
  except
end;
end;

procedure TMainForm.Home1Click(Sender: TObject);
begin
  TRY
  webbrowser.GoHome;
  except
end;
end;



procedure TMainForm.N1001Click(Sender: TObject);
begin
  webbrowser.OleObject.document.body.style.zoom:=3.0;
end;

procedure TMainForm.N501Click(Sender: TObject);
begin
  webbrowser.OleObject.document.body.style.zoom:=2.0;
end;
procedure TMainForm.RealSize1Click(Sender: TObject);
begin
  webbrowser.OleObject.document.body.style.zoom:=1.0;
end;

procedure TMainForm.N2001Click(Sender: TObject);
begin
webbrowser.OleObject.document.body.style.zoom:=4.0;
end;

procedure TMainForm.N502Click(Sender: TObject);
begin
webbrowser.OleObject.document.body.style.zoom:=0.5;
end;

procedure TMainForm.N1002Click(Sender: TObject);
begin
webbrowser.OleObject.document.body.style.zoom:=0.25;
end;

procedure TMainForm.N2002Click(Sender: TObject);
begin
webbrowser.OleObject.document.body.style.zoom:=0.15;
end;

procedure TMainForm.ScrollUp1Click(Sender: TObject);
begin
  webbrowser.OleObject.document.parentwindow.scrollby(0,-100)
end;

procedure TMainForm.ScrollDown1Click(Sender: TObject);
begin
webbrowser.OleObject.document.parentwindow.scrollby(0,+100)
end;

procedure TMainForm.ScrollLeft1Click(Sender: TObject);
begin
webbrowser.OleObject.document.parentwindow.scrollby(-100,0)
end;

procedure TMainForm.ScrollRight1Click(Sender: TObject);
begin
webbrowser.OleObject.document.parentwindow.scrollby(100,0)
end;

procedure TMainForm.CMBADDRESSKeyPress(Sender: TObject; var Key: Char);
begin
{Start searching whit press Inter key }
  if key=char(vk_return)then
  begin
    key:=#0;
    if cmbaddress.Text<>emptystr then
    begin
      webbrowser.Navigate(cmbaddress.Text);
      if cmbaddress.Items.IndexOf(cmbaddress.Text)=-1
      then cmbaddress.Items.Add(cmbaddress.Text);
    end;
{....}
end;
end;

procedure TMainForm.CMBADDRESSSelect(Sender: TObject);
  begin
{Add address to comboaddress}
    webbrowser.Navigate(cmbaddress.Text);
    if cmbaddress.Items.IndexOf(cmbaddress.Text)=-1
    then cmbaddress.Items.Add(cmbaddress.Text);
{...}
    end;

procedure TMainForm.WebBrowserDownloadComplete(Sender: TObject);
begin
  statusbar1.Panels[0].Text:='Page Download Complete.';
end;

procedure TMainForm.WebBrowserDownloadBegin(Sender: TObject);
begin
  statusbar1.Panels[0].Text:='Loading Page...';
end;
procedure TMainForm.WebBrowserProgressChange(Sender: TObject; Progress,
  ProgressMax: Integer);
begin
  if progressmax>0
    then statusbar1.Panels[0].Text:=inttostr(progress)
                                    + '  Percent From  '
                                    +inttostr(progressmax)
                                    + '  Completed.  '
                                    else statusbar1.Panels[0].Text:=' Page Download Copleted. ';
end;

procedure TMainForm.FindSomeWord1Click(Sender: TObject);
begin
  findsomeword.ShowModal;
  
end;

procedure TMainForm.About1Click(Sender: TObject);
begin
  Aboutbox.ShowModal;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
{save Comboaddress Chenges}
begin
ini:=tinifile.Create('saveaddress.ini');
try
for i:=1 to cmbaddress.Items.Count-1 do
ini.WriteString('items',cmbaddress.Items[i],cmbaddress.Items[i]);
finally
ini.Free;
end;
{....}
end;

procedure TMainForm.FormCreate(Sender: TObject);
{load last comboaddress saved}
begin
ini:=tinifile.Create('saveaddress.ini');
try
ini.ReadSection('items',cmbaddress.Items);
for i:=1 to cmbaddress.Items.Count-1 do
cmbaddress.Items[i]:=ini.ReadString('items',cmbaddress.Items[i],cmbaddress.Items[i]);
finally
ini.Free;
{...}
end;
end;

procedure TMainForm.BitBtn3Click(Sender: TObject);
begin
Aboutbox.ShowModal;
end;

procedure TMainForm.DisplayTitle1Click(Sender: TObject);
begin
showmessage('Page Title : '+webbrowser.OleObject.document.title);
end;

procedure TMainForm.DocumentReferrer1Click(Sender: TObject);
begin
showmessage('Referer : '+webbrowser.OleObject.document.referrer);
end;

procedure TMainForm.rue1Click(Sender: TObject);
begin
false1.Enabled:=true;
rue1.Enabled:=false;
listbox1.Visible:=true;
for i:=1 to webbrowser.OleObject.document.links.length-1
do
listbox1.Items.Add(webbrowser.OleObject.document.links.item(i));
end;

procedure TMainForm.False1Click(Sender: TObject);
begin
false1.Enabled:=false;
rue1.Enabled:=true;
listbox1.Visible:=false;
end;

procedure TMainForm.SetHomePage1Click(Sender: TObject);
begin
  sethome.ShowModal;
end;
procedure TMainForm.Caution1Click(Sender: TObject);
begin
cautionform.ShowModal;
end;

procedure TMainForm.Enadeled1Click(Sender: TObject);
begin
webbrowser.Offline:=true;
disabled1.Enabled:=true;
Enadeled1.Enabled:=false;
end;

procedure TMainForm.Disabled1Click(Sender: TObject);
begin
webbrowser.Offline:=False;
disabled1.Enabled:=False;
Enadeled1.Enabled:=True;
end;

procedure TMainForm.DeleteHistory1Click(Sender: TObject);
begin
deletefile('saveaddress.dll');
end;

procedure TMainForm.SaveAs1Click(Sender: TObject);
begin
if savedialog1.Execute then
begin
WB_SaveAs_HTML(webbrowser,savedialog1.FileName+'.Html');
end;
end;

procedure TMainForm.Save1Click(Sender: TObject);
begin
WB_SaveAs_HTML(webbrowser,savedialog1.FileName+'.Html');
end;



end.



